package com.mukefile.service.impl;

import com.mukefile.dao.LogDao;
import com.mukefile.dao.ManagerDao;
import com.mukefile.dao.UserDao;
import com.mukefile.pojo.Log;
import com.mukefile.pojo.MLog;
import com.mukefile.pojo.Manager;
import com.mukefile.service.LogService;
import com.mukefile.utils.GetPageInfo;
import com.mukefile.utils.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @Author: kenewstar
 * @Description:
 * @Date:Created in 2020/9/13
 */
@Service("logService")
@Transactional(rollbackFor = Exception.class)
public class LogServiceImpl implements LogService {

    @Autowired
    private LogDao logDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private ManagerDao managerDao;
    @Override
    public PageInfo<Log> findLogLimit(int pageNum, int pageSize) {
        // 页码
        // 每页的数据记录
        // 日志总记录数
        Integer count = logDao.selectCount();
        // 创建PageInfo对象
        PageInfo<Log> pageInfo = new PageInfo<>();
        // 获取limit查询参数
        GetPageInfo<Log> pi  = new GetPageInfo<Log>();
        pi.limitPage(pageNum,pageSize);
        // 执行limit查询
        List<Log> logList = logDao.selectLogByLimit(pi.getLimitStart(), pi.getLimitEnd());
        // 设置分页信息
        pageInfo.setCurrentPage(pageNum);
        pageInfo.setTotalRecords(count);
        pageInfo.setTotalPages((count%pageSize==0)?count/pageSize:count/pageSize+1);
        pageInfo.setData(logList);
        // 返回分页信息
        return pageInfo;
    }

    @Override
    public PageInfo<Log> findLogByCondition(String username, Date startTime,
                                            Date endTime, int pageNum, int pageSize) {
        Integer id = null;
        Integer id1 = userDao.selectUserIdByUsername(username);
        // 判断用户名是否为空 及 不为空串
        if (Objects.nonNull(username)&&!"".equals(username)&&Objects.nonNull(id1)){
            // 根据用户名获取用户id
            id = id1;
        }
        if (Objects.nonNull(username)&&!"".equals(username)&&Objects.isNull(id1)) {
            id = -1;
        }
        // 根据条件获取记录数
        Integer count = this.logDao.selectCountByCondition(id, startTime, endTime);
        // 执行查询
        // 获取limit查询参数
        GetPageInfo<Log> pi  = new GetPageInfo<Log>();
        pi.limitPage(pageNum,pageSize);
        List<Log> logList = logDao.selectLogByCondition(id, startTime, endTime, pi.getLimitStart(), pi.getLimitEnd());
        // 执行分页
        // 创建分页对象
        PageInfo<Log> pageInfo = new PageInfo<>();
        if (Objects.isNull(logList)){
            pageInfo.setData(new ArrayList<>());
        }else {
            pageInfo.setData(logList);
        }
        pageInfo.setCurrentPage(pageNum);
        pageInfo.setTotalRecords(count);
        pageInfo.setTotalPages((count%pageSize==0)?count/pageSize:count/pageSize+1);
        return pageInfo;
    }

    @Override
    public PageInfo<MLog> findMLogByCondition(String username, Date startTime, Date endTime, int pageNum, int pageSize) {
        Integer mid = null;
        // 根据username查询管理员id
        Manager manager = this.managerDao.selectManagerByName(username);
        if (Objects.nonNull(username)&&!"".equals(username)&&Objects.isNull(manager)){
            mid = -1;
        }
        if (Objects.nonNull(username)&&!"".equals(username)&&Objects.nonNull(manager)){
            mid = manager.getId();
        }

        // 根据条件获取记录数
        Integer count = this.logDao.selectMLogCountByCondition(mid, startTime, endTime);
        // 执行查询
        // 获取limit查询参数
        GetPageInfo<MLog> pi  = new GetPageInfo<>();
        pi.limitPage(pageNum,pageSize);
        List<MLog> mLogList = this.logDao.selectMLogByCondition(mid, startTime, endTime, pi.getLimitStart(), pi.getLimitEnd());
        // 执行分页
        // 创建分页对象
        PageInfo<MLog> pageInfo = new PageInfo<>();
        if (Objects.isNull(mLogList)){
            pageInfo.setData(new ArrayList<>());
        }else {
            pageInfo.setData(mLogList);
        }
        pageInfo.setCurrentPage(pageNum);
        pageInfo.setTotalRecords(count);
        pageInfo.setTotalPages((count%pageSize==0)?count/pageSize:count/pageSize+1);
        return pageInfo;
    }
}
